package com.situ.gym.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.situ.gym.pojo.Reserve;
import com.situ.gym.pojo.query.ReserveQuery;
import com.situ.gym.service.IReserveService;
import com.situ.gym.util.JwtUtil;
import com.situ.gym.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2025-08-22
 */
@RestController
@RequestMapping("/reserve")
public class ReserveController {
    @Autowired
    private IReserveService reserveService;

    @GetMapping("/selectAll")
    public Result<List<Reserve>> selectAll() {
        List<Reserve> list = reserveService.list();
        return Result.ok(list);
    }

    //Restful    GET(查找)、POST(添加)、DELETE(删除)、PUT(修改)
    //@RequestMapping("/list")
    //@RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    //@RequestMapping(value = "/list", method = RequestMethod.GET)
    @GetMapping("/list")
    public Result<IPage<Reserve>> list(ReserveQuery reserveQuery) {
        System.out.println("ReserveController.list");
        IPage<Reserve> page = reserveService.list(reserveQuery);
        return Result.ok(page);
    }

    // /deleteById?id=1
    // /deleteById/1
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        reserveService.removeById(id);
        return Result.ok("删除成功");
    }

    // /deleteAll/1,2,3
    @DeleteMapping("/deleteAll/{ids}")
    public Result deleteAll(@PathVariable Integer[] ids) {
        //reserveService.removeBatchByIds(Arrays.asList(ids));
        reserveService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    /*@PostMapping("/add")
    public Result add(Reserve reserve) {
        System.out.println(reserve);
        return Result.ok();
    }*/

    // @RequestBody : 封装前台传递过来的JSON格式数据
    // @ResponseBody : 将数据转换为JSON格式返回
    @PostMapping("/add")
    public Result add(@RequestBody Reserve reserve) {
        System.out.println(reserve);
        reserveService.save(reserve);
        return Result.ok("添加成功");
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Reserve reserve = reserveService.getById(id);
        return Result.ok(reserve);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Reserve reserve) {
        System.out.println(reserve);
        //reserveService.saveOrUpdate(reserve);
        reserveService.updateById(reserve);
        return Result.ok("更新成功");
    }

    @PutMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        Reserve reserve = new Reserve();
        reserve.setId(id);
        reserve.setStatus(status);
        reserveService.updateById(reserve);
        return Result.ok("修改成功");
    }
}

